//
//  YCMineViewController.m
//  YCVillageDoctor
//
//  Created by zhangxiaoliang on 2017/6/7.
//  Copyright © 2017年 zhangxiaoliang. All rights reserved.
//

#import "YCMineViewController.h"
#import "YCMineHeadrView.h"
#import "YCMineModel.h"
#import "YCMineTableViewCell.h"
#import "YCMineResultsTableViewCell.h"
#import "YCAchievementController.h"
#import "YCRepresentResultController.h"
#import "YCApplyCashViewController.h"
#import "YCApplyWithDrawController.h"
#import "YCSettingViewController.h"
#import "YCInComeModel.h"
#import "YCReadingListViewController.h"
#import "YCSalesStructureViewController.h"
#import "YCExpertInfoModel.h"
#import "YCOrderTimeModel.h"
#import "YCOrderTimeListViewController.h"

#import "MyProfileModel.h"
#import "MembershipViewController.h"

#import "UnRegisterView.h"
#import "ApplyMemberViewController.h"
#import "YCLoginViewController.h"

#import "YCPersonDataController.h"
#import "YCMyOrderViewController.h"
#import "MenberModel.h"

#import "PersonalCenterViewController.h"

@interface YCMineViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView *tableView;

@property(nonatomic,strong)NSArray *allData;

@property (nonatomic, strong)YCInComeModel *inCome;



@property (nonatomic, weak)YCAchievementController *achievement;

@property(nonatomic,strong)YCMineResultsTableViewCell *resultsCell;

@property (nonatomic ,strong)MenberModel *profile;
@property (nonatomic, strong) UnRegisterView * unregisterView;



@end

@implementation YCMineViewController

- (NSArray *)allData
{
    if (!_allData) {
        
        _allData = [YCMineModel mj_objectArrayWithFilename:@"mineModelP.plist"];
    }
    return _allData;
}

- (UnRegisterView *)unregisterView
{
    if (!_unregisterView)
    {
        self.unregisterView = [[UnRegisterView alloc] initWithCardFrame:CGRectMake(50, 150, SCREEN_WIDTH - 100, 350)];
        
    }
    
    return _unregisterView;
}



- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self stupUI];
    
#pragma mark - 获取专家信息
    NSString *expert_id= [YCUserTool sharedTools].expertID;

    NSDictionary  *params = @{
                              @"member_id":judgeString(expert_id)
                              };
    
    [DownloadData post:kGetMineInfo params:params success:^(id json) {
        
        if (json) {
            MenberModel *model = [MenberModel mj_objectWithKeyValues:json];
            self.profile = [MenberModel mj_objectWithKeyValues:json];
           YCMineHeadrView *headView = (YCMineHeadrView *)self.tableView.tableHeaderView;
            headView.model = model;
        }
    } failure:^(NSError *error) {
        
    }];
    
    NSString *link_mobile = [[NSUserDefaults standardUserDefaults]objectForKey:KLINKMOBILE];
    [DownloadData post:kGetMoneyInfo params:@{@"link_mobile":link_mobile} success:^(id json) {
        
        YCInComeModel *inCome = [YCInComeModel mj_objectWithKeyValues:json];
        
        self.inCome = inCome;

        self.achievement.inCome = self.inCome;

        _resultsCell.model = inCome;

        NSLog(@"%@",inCome);
        
    } failure:^(NSError *error) {
        
    }];
 
}

- (void)stupUI
{
    
    UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight- 64-49) style:UITableViewStyleGrouped];
    self.tableView = tableView;
    tableView.delegate = self;
    tableView.dataSource = self;
    [self.view addSubview:tableView];
    
    
    YCMineHeadrView *mineHeadrView = [[YCMineHeadrView alloc]initWithFrame:CGRectMake(0, 64,self.view.bounds.size.width, 110)];
    tableView.tableHeaderView = mineHeadrView;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapClick)];
    [mineHeadrView addGestureRecognizer:tap];
}
- (void)tapClick
{
    
    PersonalCenterViewController *personVc = [[PersonalCenterViewController alloc]init];
    personVc.menber = self.profile;
    [self.navigationController pushViewController:personVc animated:YES];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        static NSString *ID = @"mineresultscell";
        YCMineResultsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
        if (cell == nil) {
            cell = [[YCMineResultsTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        _resultsCell = cell;
        return cell;
    }else{
        static NSString *ID = @"minecell";
        YCMineTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
        if (cell == nil) {
            cell = [[YCMineTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
        }
        cell.isServiceHotline = indexPath.row == self.allData.count - 1 ? YES : NO;
        cell.model = self.allData[indexPath.row];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.allData.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        return 90;
    }
    return 45;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath


{
    NSUserDefaults *def=  [NSUserDefaults standardUserDefaults];
    NSString *expert_id= [def objectForKey:KEXPERTID];
//    NSString * check_flag = [def objectForKey:KCHECKFLAG];
//    
//     if (expert_id && [check_flag isEqualToString:@"1"]) {
    
         //@"你已经注册！而且审核通过！"
    if (expert_id) {
        switch (indexPath.row) {
                
                
                //我的业绩
            case 0:
            {
                YCAchievementController *achievement = [[YCAchievementController alloc]init];
                self.achievement = achievement;
                achievement.inCome = self.inCome;
                [self.navigationController pushViewController:achievement animated:YES];
            }
                break;
            case 1:
            { //我的订单
                NSLog(@"点击了我的订单");
                YCMyOrderViewController *orderVc = [[YCMyOrderViewController alloc]init];
                [self.navigationController pushViewController:orderVc animated:YES];
            }
                break;
                //我的销售架构
            case 2:
            {
                YCSalesStructureViewController *salesStructureVc = [[YCSalesStructureViewController alloc]init];
                [self.navigationController pushViewController:salesStructureVc animated:YES];
            }
                break;
                
            case 3:
            {
                //我的代言成果
                YCRepresentResultController *vc = [[YCRepresentResultController alloc] init];
                [self.navigationController pushViewController:vc animated:YES];
            }
                break;
                
                
            case 4:
            {
                //申请提现

                YCApplyWithDrawController * withDrawVc = [YCApplyWithDrawController new];
                
                [self.navigationController pushViewController:withDrawVc animated:YES];
                
                
            }
                break;
                
                
                
            case 5:
            {
                //设置
                YCSettingViewController *settingVc = [[YCSettingViewController alloc]init];
                [self.navigationController pushViewController:settingVc animated:YES];
            }
                break;
            case 6:
            {
                //如果已经装sim
                if ([self isSIMInstalled]) {
                    NSString *telPhone  =  @"tel://4006562020";
                    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:telPhone]];
                }else{
                    [self presentMessageTips:@"未检测到sim卡！"];
                    
                }
            }
                break;
            default:
                break;
        }

    }else{
         [UIAlertController alertTipWithTitle:@"您当前还没有登录" message:@"请您先去登录,在来看看吧！" sure:^(UIAlertAction *action) {

             [self goToLogin];

         } cancel:^(UIAlertAction *action) {

         }];
    }

    
}

//去登录
-(void)goToLogin{
    
    YCLoginViewController *loVC = [[YCLoginViewController alloc]init];
    
//    loVC.flag = @"1";
    
    [self.navigationController pushViewController:loVC animated:YES];
}


- (BOOL)isSIMInstalled
{
    CTTelephonyNetworkInfo *networkInfo = [[CTTelephonyNetworkInfo alloc] init];
    CTCarrier *carrier = [networkInfo subscriberCellularProvider];
    
    if (!carrier.isoCountryCode) {
        NSLog(@"No sim present Or No cellular coverage or phone is on airplane mode.");
        return NO;
    }
    return YES;
}
@end
